goog.require('dkpoints.errors.templates');

goog.require('goog.testing.jsunit');

var el = null;
var setUpPage = function() {
  el = document.getElementById('error_field');
};

var testGenericError = function() {
  var message = 'Random message';
  soy.renderElement(el, dkpoints.errors.templates.genericError,
                    {message: message});

  var textNode = el.firstChild.firstChild;
  assertEquals('First child should have a text node for a first child.',
               textNode.nodeType, 3);
  assertEquals('Error message text should be: ' + message,
               message, textNode.nodeValue);
};

var testHTTP404Error = function() {
  var message = 'Nothing here. Please go back.';
  soy.renderElement(el, dkpoints.errors.templates.http404, {message: message});

  var textNode = el.firstChild.firstChild;
  assertEquals('First child should have a text node for a first child.',
               textNode.nodeType, 3);
  assertEquals('Error message text should be: ' + message,
               message, textNode.nodeValue);
};


/**
 * Test that the notAuthorized template sets the correct error message for
 * authStatus & requiredAuths that shouldn't generate an error.
 */
var testNotAuthorizedScrewedUp = function() {
  var message = 'You should not be viewing this message. Please tell report ' +
                'this bug along with a screenshot and the URL you are trying ' +
                'to view. Apologies for the inconvenience.',
      params = {
        authState: 0,
        requiredAuth: 0
      },
      actualMessage;

  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 0, Required 0.',
               message, actualMessage);

  params.authState = 1;
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 1, Required 0.',
               message, actualMessage);

  params.authState = 2;
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 2, Required 0.',
               message, actualMessage);

  params.authState = 1;
  params.requiredAuth = 1;
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 1, Required 1.',
               message, actualMessage);

  params.authState = 2;
  params.requiredAuth = 1;
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 2, Required 1.',
               message, actualMessage);

  params.authState = 2;
  params.requiredAuth = 2;
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Did not show screw-up error for AuthState 2, Required 2.',
               message, actualMessage);
};


/**
 * Test that notAuthorized shows proper error messages for actual each
 * combination.
 */
var testNotAuthorizedPermutations = function() {
  var params = {},
      expectedMessage,
      actualMessage;

  // Not logged in vs. requires registered user.
  params.authState = 0;
  params.requiredAuth = 1;
  expectedMessage = 'You must log into a registered user account to view ' +
                    'this page.';
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Wrong error message for AuthState 0, Required 1.',
               expectedMessage, actualMessage);

  // Not logged in vs. requires admin user.
  params.authState = 0;
  params.requiredAuth = 2;
  expectedMessage = 'You must log into an administrator account to view this ' +
                    'page.';
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Wrong error message for AuthState 0, Required 2.',
               expectedMessage, actualMessage);

  // Regular user vs. requires admin user.
  params.authState = 1;
  params.requiredAuth = 2;
  expectedMessage = 'You are not authorized to view this page.';
  soy.renderElement(el, dkpoints.errors.templates.notAuthorized, params);
  actualMessage = el.firstChild.innerHTML;
  assertEquals('Wrong error message for AuthState 1, Required 2.',
               expectedMessage, actualMessage);
};


var testNotImplementedError = function() {
  var message = "This feature hasn't been implemented yet. Please be patient.";
  soy.renderElement(el, dkpoints.errors.templates.notImplemented, {});

  var textNode = el.firstChild.firstChild;
  assertEquals('First child should have a text node for a first child.',
               textNode.nodeType, 3);
  assertEquals('Error message text should be: ' + message,
               message, textNode.nodeValue);
};

var tearDown = function() {
  el.innerHTML = '';
};
